import config from './config.js';
const baseUrl = config.baseUrl;
var en;

function get(url, data) {
	var promise = new Promise((resolve, reject) => {
		var postData = data;
		uni.request({
			url: baseUrl + url,
			data: postData,
			method: 'GET',
			originalData: true,
			withCredentials: true,
			// dataType:"jsonp",
			// jsonp:"callbackparam",//传递给请求处理程序或页面的，用以获得jsonp回调函数名的参数名
			// jsonpCallback:"success_jsonpCallback",
			'language':  'en',
			header: {
				'content-type': 'application/json;charset=UTF-8',
				// #ifdef APP-PLUS
				'device-code': 'android-1', //谷歌上架时打开
				// #endif
				// 'token': uni.getStorageSync('token')
				
			},
			complete(res) {},
			success: function(res) {
				resolve(res.data);
				if (res.data.code == 0) {} else if (res.data.code == 403) {
					Error(res.data.msg)
					setTimeout(s => {
						uni.reLaunch({
							url: "/pages/sys/login/login"
						})

					}, 1000)
					// #ifndef APP-PLUS
					setTimeout(() => {
						this.$router.go(0)
					}, 1100)
					// #endif

				} else {
					Error(res.data.msg)
				}
			},
			error: function(e) {
				reject('Net Error');
			}
		})
	});
	return promise;
}
const Error = function(data) {
	const str = uni.getStorageSync('i18nObj')
	if (str) {
		en = JSON.parse(str)
	}
	if (en[data]) {
		uni.showToast({
			title: en[data],
			icon: "none",
			duration: 1200
		})
	} else {
		return
	}

}

function post(url, data) {
	if (url == "/ping2") {
		console.log(url)
		console.log(data.vm.$route.path)
	}
	var promise = new Promise((resolve, reject) => {
		var postData = data;
		uni.request({
			url: baseUrl + url,
			data: postData,
			method: 'POST',
			originalData: true,
			withCredentials: true,
			// dataType:"jsonp",
			// jsonp:"callbackparam",//传递给请求处理程序或页面的，用以获得jsonp回调函数名的参数名
			// jsonpCallback:"success_jsonpCallback",
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'x-requested-with': 'XMLHttpRequest',
				'X-Frame-Options': 'SAMEORIGIN',
				// #ifdef APP-PLUS
				'device-code': 'android-1', //谷歌上架时打开
				// #endif
				'language':  'en',
			},
			success: function(res) {
				resolve(res.data);

				if (res.data.code == 0) {} else if (res.data.code == 403) {
					setTimeout(s => {
						uni.reLaunch({
							url: "/pages/sys/login/login"
						})
					}, 800)
					Error(res.data.msg)
				} else {
					Error(res.data.msg)
				}
			},
			error: function(e) {
				reject('网络出错');
			}
		})
	});
	return promise;
}
export default {
	post,
	get,
	baseUrl
}